﻿namespace AddressBook.Business.Person
{
    #region "Using Statements"
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;

    using Csla;
    #endregion

    public class ContactInfo : ReadOnlyBase<ContactInfo>
    {
        #region "Business Methods"

        #region "Private Static Member Variables"

        private static PropertyInfo<int> IdProperty = RegisterProperty<int>(c => c.Id);
        private static PropertyInfo<string> FirstNameProperty = RegisterProperty<string>(c => c.FirstName);
        private static PropertyInfo<string> MiddleNameProperty = RegisterProperty<string>(c => c.MiddleName);
        private static PropertyInfo<string> LastNameProperty = RegisterProperty<string>(c => c.LastName);
        private static PropertyInfo<string> TitleProperty = RegisterProperty<string>(c => c.Title);
        private static PropertyInfo<string> SuffixProperty = RegisterProperty<string>(c => c.Suffix);
        private static PropertyInfo<string> HomeAddressPropery = RegisterProperty<string>(c => c.HomeAddress);
        private static PropertyInfo<string> HomePhonePropery = RegisterProperty<string>(c => c.HomePhone);
        private static PropertyInfo<string> CellPhonePropery = RegisterProperty<string>(c => c.CellPhone);

        #endregion

        #region "Public Properties"

        [ScaffoldColumn(false)]
        public int Id
        {
            get { return GetProperty(IdProperty); }
            private set { LoadProperty(IdProperty, value); }
        }

        public string FirstName
        {
            get { return GetProperty(FirstNameProperty); }
            private set { LoadProperty(FirstNameProperty, value); }
        }

        public string MiddleName
        {
            get { return GetProperty(MiddleNameProperty); }
            private set { LoadProperty(MiddleNameProperty, value); }
        }

        public string LastName
        {
            get { return GetProperty(LastNameProperty); }
            private set { LoadProperty(LastNameProperty, value); }
        }

        public string Title
        {
            get { return GetProperty(TitleProperty); }
            private set { LoadProperty(TitleProperty, value); }
        }

        public string Suffix
        {
            get { return GetProperty(SuffixProperty); }
            private set { LoadProperty(SuffixProperty, value); }
        }

        [ScaffoldColumn(false)]
        public string HomeAddress
        {
            get { return GetProperty(HomeAddressPropery); }
            private set { LoadProperty(HomeAddressPropery, value); }
        }

        [ScaffoldColumn(false)]
        public string HomePhone
        {
            get { return GetProperty(HomePhonePropery); }
            private set { LoadProperty(HomePhonePropery, value); }
        }

        [ScaffoldColumn(false)]
        public string CellPhone
        {
            get { return GetProperty(CellPhonePropery); }
            private set { LoadProperty(CellPhonePropery, value); }
        }

        [ScaffoldColumn(false)]
        public string FormalName
        {
            get
            {
                if (String.IsNullOrEmpty(this.MiddleName))
                {
                    return String.Format("{0} {1} {2} {3}", this.Title, this.FirstName, this.LastName, this.Suffix).Trim();
                }

                return String.Format("{0} {1} {2} {3} {4}", this.Title, this.FirstName, this.MiddleName, this.LastName, this.Suffix).Trim();
            }
        }

        [ScaffoldColumn(false)]
        public string FullName
        {
            get
            {
                return Contact.GetFullName(this.FirstName, this.MiddleName, this.LastName, this.Suffix);
            }
        }

        [ScaffoldColumn(false)]
        public string Name
        {
            get
            {
                return String.Format("{0} {1}", this.FirstName, this.LastName);
            }
        }

        #endregion

        public override string ToString()
        {
            return String.Format("{0} {1}", this.FirstName, this.LastName);
        }

        #endregion

        #region "Constructors"

        internal ContactInfo(int id, string firstName, string middleName, string lastName, string title, string suffix, AddressBook.Business.Model.ContactAddress homeAddress, AddressBook.Business.Model.ContactPhoneNumber homePhone, AddressBook.Business.Model.ContactPhoneNumber cellPhone)
        {
            Id = id;
            FirstName = firstName;
            MiddleName = middleName;
            LastName = lastName;
            Title = title;
            Suffix = suffix;

            if (homeAddress != null)
            {
                if (String.IsNullOrEmpty(homeAddress.Address.AddressLine2))
                {
                    HomeAddress = String.Format(
                        "{0} {1}, {2} {3}",
                        homeAddress.Address.AddressLine1,
                        homeAddress.Address.City,
                        homeAddress.Address.StateProvince.StateProvinceCode,
                        homeAddress.Address.PostalCode);
                }

                HomeAddress = String.Format(
                    "{0} {1} {2}, {3} {4}",
                    homeAddress.Address.AddressLine1,
                    homeAddress.Address.AddressLine2,
                    homeAddress.Address.City,
                    homeAddress.Address.StateProvince.StateProvinceCode,
                    homeAddress.Address.PostalCode);
            }
            else
            {
                HomeAddress = String.Empty;
            }

            if (homePhone != null)
            {
                HomePhone = PhoneNumber.FormatPhone(homePhone.PhoneNumber.PhoneNumber1);
            }
            else
            {
                HomePhone = String.Empty;
            }

            if (cellPhone != null)
            {
                CellPhone = PhoneNumber.FormatPhone(cellPhone.PhoneNumber.PhoneNumber1);
            }
            else
            {
                CellPhone = String.Empty;
            }
        }

        #endregion
    }
}
